package com.vchello.controller.message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.plugins.Page;
import com.vchello.bean.message.Message;
import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.controller.constants.SubjectAttrConstants;
import com.vchello.service.message.MessageService;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/message")
public class MessageController extends BaseController {
	
	private static final String REDIRECT = "redirect:/login";
	private static final int STATUS = 1;
	private static final int ISSUCCESS = 1;
	
	@Autowired
	private MessageService messageService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model){
		return null;
	}
	
	@RequestMapping(value = "/sendList", method = RequestMethod.GET)
	public String sendList(Model model, Page<Message> page){
		User user = getUser();
		if(null == user) {
			return REDIRECT;
		}
		page = messageService.findMessageBySend(page, user.getId());
		model.addAttribute("page", page);
		return "message/sendMessage";
		
	}
	
	@RequestMapping(value = "/sendListQuery", method = RequestMethod.POST)
	public String sendListQuery(Model model, Page<Message> page){
		User user = getUser();
		if(null == user) {
			return REDIRECT;
		}
		page = messageService.findMessageBySend(page, user.getId());
		model.addAttribute("page", page);
		return "message/query/sendQuery";
		
	}
	
	@RequestMapping(value = "/receiveList", method = RequestMethod.GET)
	public String receiveList(Model model, Page<Message> page){
		User user = getUser();
		if(null == user) {
			return REDIRECT;
		}
		page = messageService.findMessageByReceive(page, user.getId());
		messageService.updateRead(user.getId());
		session.setAttribute(SubjectAttrConstants.MESSAGE_COUNT, 0); //重置消息提醒
		model.addAttribute("page", page);
		return "message/receiveMessage";
		
	}
	
	@RequestMapping(value = "/receiveListQuery", method = RequestMethod.POST)
	public String receiveListQuery(Model model, Page<Message> page){
		User user = getUser();
		if(null == user) {
			return REDIRECT;
		}
		page = messageService.findMessageByReceive(page, user.getId());
		messageService.updateRead(user.getId());
		model.addAttribute("page", page);
		return "message/query/receiveQuery";
		
	}
	
	@RequestMapping(value = "/dustbinList", method = RequestMethod.GET)
	public String dustbinList(Model model, Page<Message> page){
		User user = getUser();
		if(null == user) {
			return REDIRECT;
		}
		page = messageService.findMessageByStatus(page, user.getId());
		model.addAttribute("page", page);
		return "message/dustbinMessage";
		
	}
	
	@RequestMapping(value = "/dustbinListQuery", method = RequestMethod.POST)
	public String dustbinListQuery(Model model, Page<Message> page){
		User user = getUser();
		if(null == user) {
			return REDIRECT;
		}
		page = messageService.findMessageByStatus(page, user.getId());
		model.addAttribute("page", page);
		return "message/query/dustbinQuery";
		
	}
	
	@RequestMapping(value = "/addMessage", method = RequestMethod.GET)
	public String addMessage(Model model) {
		User user = getUser();
		if(null == user) {
			return REDIRECT;
		}
		int receiveCount = messageService.receiveMessageCount(user.getId());
		model.addAttribute("receiveCount", receiveCount);
		return "message/addMessage";
		
	}
	
	@RequestMapping(value = "/addMessage", method = RequestMethod.POST)
	public void addMessage(Message message, String userId) {
		try {
			User user = getUser();
			if(null == user) {
				error("用户未登录");
				return;
			}
			String[] userArray = userId.split(",");
			Message addMessage = null;
			for (int i = 0; i < userArray.length; i++) {
				addMessage = new Message();
				addMessage.setSendUserId(user.getId());
				addMessage.setReceiveUserId(Integer.parseInt(userArray[i]));
				addMessage.setTitle(message.getTitle());
				addMessage.setContent(message.getContent());
				addMessage.setCreateTime(Utility.getSimpleFormatedDayNow());
				addMessage.setStatus(STATUS);
				addMessage.setIsSuccess(ISSUCCESS);
				messageService.saveMessage(addMessage);
			}
			success("消息发送成功");
		} catch (Exception e) {
			e.printStackTrace();
			error("消息发送失败");
		}
	}
	
	@RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
	public void updateStatus(int id){
		boolean isTrue = messageService.updateStatus(id);
		if(isTrue) {
			success("已移除到垃圾箱");
			return;
		}
		error("移除垃圾箱失败");
	}
	
	@RequestMapping(value = "/deleteMessage", method = RequestMethod.POST)
	public void deleteMessage(int id){
		Message message = messageService.findById(id);
		boolean isTrue = messageService.deleteMessage(message);
		if(isTrue) {
			success("删除消息成功");
			return;
		}
		error("删除消息失败");
	}
}
